package com.xinmao.quantitative.trad.strategies.combine;

import com.xinmao.quantitative.trad.strategies.IStrategy;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Rule;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.ichimoku.*;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.UnderIndicatorRule;

/**
 * @Title: IchimokuCloudStrtegy
 * @Author: XYe13
 * @Date: 5/22/2024 13:54
 * @Description: Ichimoku Kinko Hyo
 * @see <a href="https://school.stockcharts.com/doku.php?id=technical_indicators:ichimoku_cloud">Ichimoku Cloud</a>
 */
public class IchimokuCloudStrategy implements IStrategy {

    @Override
    public Strategy buildStrategy(BarSeries series) {
        //转换线
        IchimokuTenkanSenIndicator tenkanSen = new IchimokuTenkanSenIndicator(series, 5);
        //基线
        IchimokuKijunSenIndicator kijunSen = new IchimokuKijunSenIndicator(series, 13);
        //领先跨度 A
        IchimokuSenkouSpanAIndicator senkouSpanA = new IchimokuSenkouSpanAIndicator(series, tenkanSen, kijunSen, 13);
        //领先跨度 B
        IchimokuSenkouSpanBIndicator senkouSpanB = new IchimokuSenkouSpanBIndicator(series, 26, 13);
        //滞后跨度
        IchimokuChikouSpanIndicator chikouSpan = new IchimokuChikouSpanIndicator(series, 13);

        ClosePriceIndicator closePrice = new ClosePriceIndicator(series);

        //价格走势高于云（趋势）
        //云从红色变为绿色（趋势内的潮起潮落）
        //价格高于基线（动量）
        //转换线移动到基线上方（动量）
        //价格穿过转换线（动量）
        //价格高于滞后线（动量）
        //经典信号是寻找转换线以越过基线。虽然这个信号可能是有效的，但在强劲趋势中也可能很少见。通过寻找价格以越过基线（甚至转换线），可以找到更多信号。
        Rule enterRule = new OverIndicatorRule(closePrice, senkouSpanA)
                .and(new OverIndicatorRule(senkouSpanA, senkouSpanB))
                .and(new OverIndicatorRule(closePrice, kijunSen))
                .and(new OverIndicatorRule(tenkanSen, kijunSen))
                .and(new OverIndicatorRule(closePrice, tenkanSen))
                .and(new OverIndicatorRule(chikouSpan, kijunSen));

        //价格跌破云层（趋势）
        //云从绿色变为红色（趋势内的潮起潮落）
        //价格低于基线（动量）
        //转换线移动到基线下方（动量）
        //价格跌穿过转换线（动量）
        //滞后线低于基线（动量）
        Rule exitRule = new UnderIndicatorRule(closePrice, senkouSpanB)
                .and(new UnderIndicatorRule(senkouSpanA,senkouSpanB))
                .and(new UnderIndicatorRule(closePrice,kijunSen))
                .and(new UnderIndicatorRule(tenkanSen,kijunSen))
                .and(new UnderIndicatorRule(closePrice, tenkanSen))
                .and(new UnderIndicatorRule(chikouSpan, kijunSen));

        return new BaseStrategy("Ichimoku Cloud Strategy", enterRule, exitRule);
    }

    @Override
    public Num getScore() {
        return DecimalNum.valueOf(1);
    }
}
